package com.newer.controller.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newer.pojo.sys.Organization;
import com.newer.service.sys.iter.OrganizationService;
import com.newer.utils.sys.BootstrapDataTable;
import com.newer.utils.sys.MsgUtil;

@RequestMapping("organization")
@Controller
public class OrganizationController {
	@Autowired
	private OrganizationService organizationService;

	/**
	 * 显示资源列表
	 * 
	 * @param bootstrapDataTable
	 * @param conditions
	 * @return
	 */
	@RequestMapping("tableView")
	public @ResponseBody BootstrapDataTable tableView(BootstrapDataTable bootstrapDataTable) {
		return organizationService.tableView(bootstrapDataTable);
	}

	/**
	 * 根据id查询，跳转到修改页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("selectOrganizationById/{id}")
	public String selectOrganizationById(@PathVariable("id")Integer id,HttpServletRequest request){
		request.setAttribute("organization", organizationService.selectOrganizationById(id));
		return "sys/organization/organization_edit";
	}

	/**
	 * 修改部门信息
	 * @param organization
	 * @return
	 */
	@RequestMapping("update")
	public @ResponseBody MsgUtil toUpdateOrganization(Organization organization) {
		try {
			organizationService.updateOrganizationByPrimaryKey(organization);
			return	MsgUtil.result(true, "修改成功");
		} catch (Exception e) {
			return	MsgUtil.result(false, "修改失败");
		}
	}


	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping("delete/{id}")
	public @ResponseBody MsgUtil deleteOrganization(@PathVariable("id") Integer id) {
		System.out.println(id);
		List<Integer> list = new ArrayList<Integer>();
		list.add(id);
		// 执行删除
		boolean result = organizationService.deleteOrganization(list);
		if (result == true) {
			return MsgUtil.result(result, "删除成功");
		} else {
			return MsgUtil.result(result, "删除失败");
		}
	}

	/**
	 * 跳转到部门添加页面
	 * @return
	 */
	@RequestMapping("add")
	public String add(){
		return "sys/organization/organization_add";
	}


	/**
	 * 添加部门
	 * @param organization
	 * @return
	 */
	@RequestMapping("addOrganization")
	public @ResponseBody MsgUtil addOrganization(Organization organization) {
		try {
			organizationService.addOrganization(organization);
			return MsgUtil.result(true, "添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return MsgUtil.result(false, "添加失败！");
		}
	}

	/**
	 * 检查部门是否存在
	 * @param organizationName
	 * @param id
	 * @return
	 */
	@RequestMapping("checkOrganizationName")
	public @ResponseBody Map<String,Boolean> checkOrganizationName(String organizationName){
		Map<String, Boolean> selecorganizationName =organizationService.selectOrganizationByOrganizationName(organizationName);
		return selecorganizationName;
	}
}
